<template>
	<el-dialog title="个人日程" v-model="visible" :width="800" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="日程标题" prop="title">
				<el-input v-model="form.title" placeholder="日程标题" clearable></el-input>
			</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="事务类型" prop="calendarType">
						<sc-select v-model="form.calendarType" :params="{code:'calendar'}" :apiObj="$API.system.sysDic.getSysDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="领导可见">
						<el-switch v-model="form.shareStatus" active-value="1" inactive-value="0"></el-switch>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="日程开始" prop="startTime">
						<el-date-picker style="width: 100%"
							:disabled-date="publishDateAfter"
							v-model="form.startTime"
							type="datetime"
							value-format="YYYY-MM-DD HH:mm:ss"
						>
						</el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="日程结束" prop="endTime">
						<el-date-picker style="width: 100%"
							:disabled-date="publishDateAfter"
							v-model="form.endTime"
							type="datetime"
							value-format="YYYY-MM-DD HH:mm:ss"
						>
						</el-date-picker>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="日程级别" prop="eventLevel">
						<sc-select v-model="form.eventLevel" :params="{code:'calendarLevel'}" :apiObj="$API.system.sysDic.getSysDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="提醒时机" prop="advance">
						<el-select v-model="form.advance" style="width: 100%">
							<el-option v-for="item in this.advanceList" :value="item.value" :label="item.label"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="form.remark" maxlength="200" clearable type="textarea"></el-input>
			</el-form-item>
			<msg-box-check-box v-model:msgType="form.msgType" code="calendar"></msg-box-check-box>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode=='edit'" type="danger" :loading="isSaveing" @click="deleteEvent()">删 除</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
import msgBoxCheckBox from "@/views/module/msgBoxCheckBox";
export default {
	components: {msgBoxCheckBox},
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			visible: false,
			isSaveing: false,
			fileList: [],
			//表单数据
			form: {
				calendarId: '',
				title:'',
				startTime:'',
				endTime:'',
				shareStatus: '',
				calendarType:'',
				status: 1,
				eventLevel:'',
				advance: '',
				remark: ''
			},
			advanceList:[],
			//验证规则
			rules: {
				title: [
					{required: true, message: '日程标题不能为空'}
				],
				startTime: [
					{required: true, message: '日程开始时间不能为空'}
				],
				endTime: [
					{required: true, message: '日程结束时间不能为空'}
				],
				calendarType: [
					{required: true, message: '请选择日程类型'}
				]
			}
		}
	},
	mounted() {
		this.getSysDicByCode();
	},
	methods: {
		publishDateAfter(time){
			return time.getTime() < (Date.now()-24*60*60*1000);
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			if (this.mode == "add") {
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res = await this.$API.calendar.insertCalendar.post(this.form);
						this.isSaveing = false;
						if (res.code == 200) {
							this.$emit('success')
							this.visible = false;
							this.$message.success("操作成功")
						} else {
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			} else if (this.mode == "edit") {
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res = await this.$API.calendar.updateCalendar.post(this.form);
						this.isSaveing = false;
						if (res.code == 200) {
							this.visible = false;
							this.$emit('success')
							this.$message.success("操作成功")
						} else {
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			}
		},
		async getSysDicByCode(){
			let res = await this.$API.system.sysDic.getSysDicByCode.get({code:'advance'})
			if (res.code == 200) {
				this.advanceList=res.data;
			}
		},
		async getCalendarById(calendarId)
		{
			let res = await this.$API.calendar.getCalendarById.get({calendarId:calendarId})
			if (res.code == 200) {
				this.form.calendarId = res.data.calendarId
				this.form.title = res.data.title
				this.form.startTime= res.data.startTime
				this.form.endTime = res.data.endTime
				this.form.shareStatus = res.data.shareStatus
				this.form.calendarType = res.data.calendarType
				this.form.status = res.data.status
				this.form.eventLevel = res.data.eventLevel
				this.form.advance = res.data.advance
				this.form.remark = res.data.remark
			}
		},
		async deleteEvent()
		{
			if(this.form.calendarId=="")
			{
				this.$alert("删除失败", "提示", {type: 'error'})
				return false;
			}else
			{
				this.isSaveing = true;
				var res = await this.$API.calendar.deleteCalendar.post({calendarId:this.form.calendarId});
				this.isSaveing = false;
				if (res.code == 200) {
					this.visible = false;
					this.$emit('success')
					this.$message.success("操作成功")
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
			}
		},
		setData(data) {
			this.form.eventLevel = "0";
			this.form.calendarType = "0";
			this.form.advance = "0";
			this.form.startTime= data.start
			this.form.endTime = data.end
			if(data.calendarId!=undefined&&data.calendarId!="")
			{
				this.getCalendarById(data.calendarId);
			}
		}
	}
}
</script>

<style>
</style>
